package core.weapons;
import graphics.GlowParticle;
import graphics.Particle;
import graphics.SparkParticle;

import java.awt.*;

import core.Player;
import core.units.Unit;
import core.world.CollisionHandler;

public class Missile extends Attack {
	double dir;
	
	//protected final int FACTOR=10;

    public Missile(int x1, int y1, Player m, double inDir) {
    	super(x1, y1, m);
    	dir=inDir;
    	speed=3000;
    	lifetime = 600;
    	damage=75;
    	x=x;
    	y=y;
    	
    }
    public void checkHit(Unit u){
		if(hasHit==false && u.getTeam()!=team && u.bounds.contains(x, y)){
			u.takeDamage(damage);
			hasHit=true;
			//System.out.print("i");
		}			
	}
	public void nextFrame(){
		double targetAngle=angleTo(findNearestEnemy());
		if(dir>1)
			dir=dir-1;
		if(dir<0)
			dir=dir+1;
		if(targetAngle<0)
			targetAngle=targetAngle+1;
		if(targetAngle>1)
			targetAngle=targetAngle-1;
			
		if(targetAngle>dir&&targetAngle-dir<0.5 || dir-targetAngle>0.5)
			dir=dir+0.003;
		else
			dir=dir-0.003;
		
		
			
		
		lifetime--;
		if(lifetime==0 || hasHit==true){
			createExplosionParticles();
			main.attacks.remove(this);		
			
		}
		

		int oldX=x;
		int oldY=y;
		int dx = (int)(Math.cos(dir*3.1415*2)*speed);
		int dy = (int)(Math.sin(dir*3.1415*2)*speed);
		x=x+dx;
		y=y+dy;
		if(CollisionHandler.intersects(new Point(oldX, oldY), new Point(x, y))){
			createExplosionParticles();
			main.attacks.remove(this);
		}
		
		int particleCount = 10+damage/3;
		for(int i=0; i<particleCount; i++){
			//main.particles.add(new Particle(x, y, 0, 0));
			Particle p = new GlowParticle((int)x-dx*i/particleCount, (int)y-dy*i/particleCount);			
			p.setImage("glowYellow1.png");
			main.particles.add(p);			
		}
		
		
	}

	public void paint(Point screenPos, int zoomDivider, Graphics g){
		g.setColor(Color.gray);
		int size=0;
		g.fillRect((x-screenPos.x-size/2)/zoomDivider, (y-screenPos.y-size/2)/zoomDivider, size/zoomDivider, size/zoomDivider);
	}
	
	private void createExplosionParticles(){
		for(int i=0; i<damage; i++)
			main.particles.add(new SparkParticle(x, y, (int)(Math.random()*6000-3000), (int)(Math.random()*6000-3000)));
	}
	
}
